<template>
  <!-- 弹出层 -- 部门 -->
  <el-dialog :title="title" :visible="dialogFormVisible" @close="resetForm">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name" size="mini">
        <el-input v-model="form.name" style="width:80%" placeholder="" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code" size="mini">
        <el-input v-model="form.code" style="width:80%" placeholder="" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId" size="mini">
        <el-select v-model="form.managerId" style="width:80%" placeholder="">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce" size="mini" :rows="4">
        <el-input v-model="form.introduce" style="width:80%" type="textarea" placeholder="" />
      </el-form-item>
    </el-form>
    <div slot="footer" style="text-align: center;">
      <el-button size="mini" type="primary" @click="onSubmit">确 定</el-button>
      <el-button size="mini" @click="resetForm">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDetial, updateDepartments, getEmployeeSimple } from '@/api/department'
export default {
  name: 'Department',
  props: {
    // 弹出层类型
    type: {
      type: String,
      default: ''
    },
    // 弹出层显示隐藏
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    // 部门所属id
    id: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      // 部门新增数据
      form: {
        code: '',
        introduce: '',
        managerId: null,
        name: '',
        pid: null
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          {
            trigger: 'blur',
            // 自定义校验模式
            validator: async(rule, value, callback) => {
              // value就是输入的名称
              let result = await getDepartments()
              if (this.form.id) {
                // 编辑场景 排除自身
                result = result.filter(item => item.id !== this.form.id)
              }
              // result数组中是否存在 value值
              if (result.some(item => item.name === value)) {
                callback(new Error('部门中已经有该名称了'))
              } else {
                callback()
              }
            }
          }

        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          {
            trigger: 'blur',
            // 自定义校验模式
            validator: async(rule, value, callback) => {
              // value就是输入的编码
              let result = await getDepartments()
              // 判断是否是编辑模式
              if (this.form.id) {
                // 编辑场景
                result = result.filter(item => item.id !== this.form.id)
              }
              // result数组中是否存在 value值
              if (result.some(item => item.code === value)) {
                callback(new Error('部门中已经有该编码了'))
              } else {
                callback()
              }
            }
          }
        ],
        managerId: [
          { required: true, message: '请选择部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' }
        ]
      },
      managerList: []
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  async created() {
    // 获取员工列表
    this.managerList = await getEmployeeSimple()
  },
  methods: {
    // 添加部门
    async onSubmit() {
      // 表单验证
      await this.$refs.form.validate()
      if (!this.form.id) {
        this.form.pid = this.id
        await addDepartments(this.form)
      } else {
        await updateDepartments(this.form)
      }
      // 清空表单
      this.resetForm()
      // 重新渲染列表
      this.$emit('onSubmit')
    },
    // 获取部门详情
    async getDetialFn() {
      // console.log(id, this.type)
      const res = await getDetial(this.id)
      // console.log(res)
      this.form = { ...res }
    },
    // 清空表单并关闭弹出
    resetForm() {
      this.$refs.form.resetFields()
      this.form = {
        code: '',
        introduce: '',
        managerId: null,
        name: '',
        pid: null
      }
      this.$emit('update:dialogFormVisible', false)
    }
  }
}
</script>

